// domaindetail.js  域见二级页
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    domainlist: [],
    currentTab: 0,
    listflag: true,
    has_more: true,
    showLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      province_id: options.province_id,
      banner: options.topimg
    });
    this.getDomain();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  getDomain: function () {
    var that = this;
    shihan.sendRequest(config.domain_list, 'GET', {
      province_id: that.data.province_id,
      page: that.data.page,
      page_size: that.data.page_size
    }, {})
      .then(function (response) {
        if (response.data.code == 0 && response.data.data.sh_brands.length > 0) {
          that.setData({
            listflag: true,
            has_more: true,
            page: that.data.page + 1,
            domainlist: that.data.domainlist.concat(response.data.data.sh_brands)
          });
          if (that.data.domainlist.length == response.data.data.sh_total_rows) {
            that.setData({
              listflag: false,
              has_more: false
            })
          } 
        } else {
          that.setData({
            listflag: false,
            has_more: false
          })
        }
        that.setData({
          showLoading: false,
          banner_tit: response.data.data.sh_brand_province || ''
        })
      }, function (error) {
        console.log(error);
      });
  },
  onNavbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    });
  },
  openstoredetail: function (e) {
    wx.navigateTo({
      url: '/pages/storedetail/storedetail?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      page_size: 20,
      domainlist: [],
      listflag: true,
      has_more: true
    })
    this.getDomain()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.listflag) {
      this.setData({
        listflag: false
      })
      this.getDomain()
    }
  }
})